package com.qqbot.impl.listener;

import com.qqbot.core.core.*;
import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.service.DuJiaoShouApiService;
import com.qqbot.impl.service.JiuLiApiService;
import com.qqbot.impl.service.LingDuApiService;
import com.qqbot.impl.service.XiaoBaiApiService;
import com.qqbot.impl.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.qqbot.core.annotation.GroupMessageHandler;

import java.util.List;

/**
 * 监听
 *
 * @author changwenpeng
 * @date 2022年5月9日
 */
@Component
public class MyListener {

    @Autowired
    DuJiaoShouApiService duJiaoShouApiService;

    @Autowired
    JiuLiApiService jiuLiApiService;

    @Autowired
    LingDuApiService lingDuApiService;

    @Autowired
    XiaoBaiApiService xiaoBaiApiService;

    @Autowired
    RedisUtils redisUtils;

    /**
     * 帮助
     *
     * @param group
     * @param member
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = {727289809, 478728208}, regex = "^帮助$")
    public void talk(Group group, Member member) throws Exception {
        MessageChain mc = new MessageChain();
        mc.at(member.getUserId()).text("\n")
                .text("1. @我 + [聊天内容]").text("\n")
                .text("2. 点歌 + [大风吹]").text("\n")
                .text("3. 解析视频 + [APP内复制的地址]（抖音、皮皮虾、微视、最右、火山、今日头条、快手、网易视频、西瓜、皮皮搞笑、美拍、哔哩哔哩、A站、B站）").text("\n")
                .text("4. [网红]+视频（小姐姐/网红）").text("\n")
                .text("5. [女神]+视频（游戏、动漫、动物、风景、情侣、姓氏特效、酷炫特效、动态壁纸、热歌、女神、男神、明星、节日、充电、闹钟）").text("\n")
                .text("6. 定时任务：每日新闻早知道【不可操作】").text("\n")
                .text("7. 定时任务：每日早安，晚安【不可操作】").text("\n")
                .text("8. 定时任务：每日八杯水【不可操作】").text("\n")
                .text("9. 基本截图操作【不支持截图绑定，表情截图，不支持上号】").text("\n")
                .text("10. 抓 + [@目标]").text("\n")
                .text("11. 啃 + [@目标]").text("\n")
                .text("12. 锤 + [@目标]");
        group.sendMessage(mc);
    }

    /**
     * 点歌
     *
     * @param group
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = {727289809, 478728208}, regex = "^点歌.*$")
    public void music(Group group, Member member, MessageChain mc) throws Exception {
        String keyword = mc.getText();
        if (!StringUtils.hasText(keyword)) {
            return;
        }
        keyword = keyword.replaceAll("\\s*", "").substring(2);
        group.sendMessage(duJiaoShouApiService.musicCustom(keyword, member.getNickname()));
    }

    /**
     * 随机小视频
     *
     * @param group
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = {727289809, 478728208}, regex = "^(小姐姐|网红)(视频)$")
    public void video(Group group, Member member, MessageChain mc) throws Exception {
        String keyword = mc.getText();
        if (!StringUtils.hasText(keyword)) {
            return;
        }
        keyword = keyword.replaceAll("\\s*", "").substring(0, keyword.length() - 2);
        group.sendMessage(duJiaoShouApiService.video(keyword));
    }

    /**
     * 随机小视频2
     *
     * @param group
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = {727289809, 478728208}, regex = "^(游戏|动漫|动物|风景|情侣|姓氏特效|酷炫特效|动态壁纸" +
            "|热歌|女神|男神|明星|节日|充电|闹钟)(视频)$")
    public void video2(Group group, Member member, MessageChain mc) throws Exception {
        String keyword = mc.getText();
        if (!StringUtils.hasText(keyword)) {
            return;
        }
        keyword = keyword.replaceAll("\\s*", "").substring(0, keyword.length() - 2);
        group.sendMessage(xiaoBaiApiService.video(keyword));
    }

    /**
     * 解析视频
     *
     * @param group
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = {727289809, 478728208}, regex = "^解析视频.*$")
    public void jieXi(Group group, Member member, MessageChain mc) throws Exception {
        String keyword = mc.getText();
        if (!StringUtils.hasText(keyword)) {
            return;
        }
        keyword = keyword.replaceAll("\\s*", "").substring(4);
        group.sendMessage(lingDuApiService.jieXi(keyword, member.getUserId()));
    }

    /**
     * 小爱同学
     *
     * @param group
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = {727289809, 478728208}, isAt = true)
    public void xiaoAi(Group group, Member member, MessageChain mc) throws Exception {
        String keyword = mc.getText();
        if (!StringUtils.hasText(keyword)) {
            return;
        }
        keyword = keyword.replaceAll("\\s*", "");
        group.sendMessage(jiuLiApiService.xiaoAi(keyword));
    }

    /**
     * 锤
     * @param group
     * @param member
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = {727289809, 478728208}, regex = "^锤$")
    public void headChui(Group group, Member member, MessageChain mc) throws Exception {
        String keyword = mc.getTextAndAt();
        if (!StringUtils.hasText(keyword)) {
            return;
        }
        if(keyword.length() == 1){
            keyword = String.valueOf(member.getUserId());
        }else{
            keyword = keyword.substring(1).replaceAll("\\s*", "");
        }
        group.sendMessage(xiaoBaiApiService.headChui(keyword));
    }

    /**
     * 啃
     * @param group
     * @param member
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = {727289809, 478728208}, regex = "^啃$")
    public void headKen(Group group, Member member, MessageChain mc) throws Exception {
        String keyword = mc.getTextAndAt();
        if (!StringUtils.hasText(keyword)) {
            return;
        }
        if(keyword.length() == 1){
            keyword = String.valueOf(member.getUserId());
        }else{
            keyword = keyword.substring(1).replaceAll("\\s*", "");
        }
        group.sendMessage(xiaoBaiApiService.headken(keyword));
    }

    /**
     * 抓
     * @param group
     * @param member
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = {727289809, 478728208}, regex = "^抓$")
    public void headZhua(Group group, Member member, MessageChain mc) throws Exception {
        String keyword = mc.getTextAndAt();
        if (!StringUtils.hasText(keyword)) {
            return;
        }
        if(keyword.length() == 1){
            keyword = String.valueOf(member.getUserId());
        }else{
            keyword = keyword.substring(1).replaceAll("\\s*", "");
        }
        group.sendMessage(xiaoBaiApiService.headZhua(keyword));
    }

    /**
     * 上号
     * @param group
     * @param member
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = 456088603, isAt = true)
    public void shangHao(Group group, Member member, MessageChain mc) throws Exception {
        String msg = mc.getText();
        System.out.println(msg);
        group.getBot().sendPrivateMessage(201088830, mc);
    }

    /**
     * 记录最后一次聊天存入redis
     * @param group
     * @param member
     * @param mc
     * @throws Exception
     */
    @GroupMessageHandler(groupIds = 478728208)
    public void LongTimeTalk(Group group, Member member, MessageChain mc) throws Exception {
        long userId = member.getUserId();
        long groupId = group.getGroupId();
        if(userId == group.getBot().getUserId()){
            return;
        }
        String key = groupId + "" + userId;
        redisUtils.add(key, System.currentTimeMillis());
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        String a = "^解析视频.*$";
        System.out.println("解析视频123465".matches(a));

        String b = "<html></html>ssss";
        int mp4 = b.indexOf("</html>");
        System.out.println(b.substring(mp4 + 7, b.length()));

        System.out.println(new java.util.Random().nextBoolean() ? 2 : 1);

    }
}
